package com.monkey.controller;

import com.monkey.utils.QiniuUtils;
import com.monkey.vo.Results;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author：monkey-L
 * @description: .
 * @date: 20:42 2021/10/6
 */
@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private QiniuUtils qiniuUtils;

    @PostMapping
    public Results upload(@RequestParam("image") MultipartFile file) {
        //拿到上传的文件名
        String originalFilename = file.getOriginalFilename();
        //唯一的文件名称
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        //上传文件，上传到七牛云服务器，按量付费，速度快，把图片放到离用户最近的服务器上
        //降低我们自身应用服务器的带宽消耗
        boolean upload = qiniuUtils.upload(file,fileName);
        if (upload) {
            return Results.success(QiniuUtils.url+fileName);
        }
        return Results.fail(20001, "上传失败");
    }
}
